<template>
	<div class="main" ref='remove'>
		<div v-transfer-dom>
			<popup v-model="showselect3" position="right">

				<div style="width:7.5rem;height: 100vh;" class="selectNews4Wrap">
					<div class="top flex-h" v-on:click="clearShow">

						<img class="imgIcon" src="~/static/iconsvgs/top_icon_back.png" alt="">
						<span class="flex1 univTitle"></span>
					</div>
					<div style="width: 100%;height:auto;">


						<scroller @on-scroll-bottom="oninfinite" lock-x ref="scrollerBottom">

							<div class="wdBoxOutWrap">
								<div class="wdBoxOuter">
									<question-answer :quesData="wdData" v-on:listToRewd='listFromPare'></question-answer>
								</div>

								<load-more :tip="loadtxt" :show-loading="loads"></load-more>
								<div style="height: 0.8rem;"></div>
							</div>

						</scroller>
					</div>
				</div>
			</popup>
		</div>
		<select-desc :showWho='whos' v-on:clearShowDesc="clearDesc" :showselect2='showsdescor' :reData='remeData' :authorData="authorDatas"
		 :remTitle="remTitles"></select-desc>
	</div>
</template>
<script> 
	import {
		TransferDom,
		Popup,
		Scroller,
		LoadMore 

	} from 'vux'
	import goBack from '~/components/goBack'
	import { getAnswerService, geQuestionAnswer } from '~/api/school'
	import questionAnswer from '~/components/questionAnswer'
	import selectDesc from '~/components/selectNews2'
	import ls from 'store2'
	import {
		format
	} from '~/plugins/filter';
	export default {
// 		head: {
// 			title: ''
// 		},
		directives: {
			TransferDom
		},
		components: {
			TransferDom,
			Popup,
			questionAnswer,
			selectDesc,
			Scroller,
			LoadMore



		},
		props: {
			showselect3: {
				type: Boolean,
				default: false
			},
			botNum: {
				type: Number,
				default: 1
			},
			bottomCounts: {
				type: Number,
				default: 1
			}

		},
		data() {
			return {
				textTxt: '',
				wdData: [],
				wdLens: 1,
				showsdescor: false,
				remTitles: '',
				remeData: [],
				whos: '',
				authorDatas: {},
				bottomCount: this.bottomCounts * 1,
				loads: true,
				loadtxt: '正在加载中',
				totalNum: 10
			}

		},
		mounted: function() {
			// this.showWdList()



		},
		destroyed() {},
		watch: {

		},
		methods: {
			isTerminal() {
				var u = navigator.userAgent;
				var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
				var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
				if (isAndroid) {
					return "isAndroid"
				} else if (isiOS) {
					return "isiOS"
				}
			},
			showAnBack() {
				if (this.isTerminal() === 'isAndroid') {
					
					if (window.history && window.history.pushState) {
						history.pushState(null, null, document.URL);
						window.addEventListener('popstate', this.fun, false); //false阻止默认事件
					}
				}

			},
			fun() {
				// console.log('返回')
				this.showsdescor = false
				setTimeout(() => {
					this.removeback()
				}, 50)

			},
			removeback() {
				window.removeEventListener('popstate', this.fun, false);
			},

			oninfinite() {
				if (this.onFetching) {
					// do nothing
				} else {
					this.onFetching = true
					setTimeout(() => {
						this.bottomCount += 1
						// console.log('543112')
						if (this.bottomCount < this.totalNum + 1 && this.totalNum > 1) {
							this.$nextTick(() => {
								this.$refs.scrollerBottom.reset()
								this.showWdList()
							})
						} else {
							this.loads = false;
							this.loadtxt = '没有更多数据了~'
						}

						this.onFetching = false
					}, 2000)
				}

			},


			clearDesc() {
				this.showsdescor = false
				this.removeback()
			},
			listFromPare(data) {
				console.log('daas', data.author.avatar)
				if (data.status > 0) {
					this.whos = "rmwd"
					this.remTitles = data.title
					let obj = {
						"title": data.title,
						"createTime": data.createTime,
						"avatar": data.author.avatar,
						"name": data.author.name,
						"status": data.status,
						"answerCount": data.answer_count,
						
					}
					console.log('sss',obj)
					this.authorDatas = obj
					getAnswerService(data.questionId).then(res => {
						if (res.data) {
							this.remeData = res.data
						}
						this.$vux.loading.hide();
					}).catch((error) => {
						// this.$vux.toast.text('暂无数据');
						this.$vux.loading.hide();
					})

					setTimeout(() => {
						this.showsdescor = true
						this.showAnBack();
					}, 50)

				} 
			},
			showWdList() {
				geQuestionAnswer({
					"academyCode": ls.get('applsData').academyCode + "",
					"page":this.bottomCount,
					"limit": 10
				
				}).then(res => {
					if (this.bottomCount === 1) {
						this.wdData = res.data
					} else {
						res.data.forEach(item => {
							this.wdData.push(item)
						})
					}
					ls.set('botnum', this.bottomCount)
					this.totalNum = Math.ceil(res.meta.total / 10);
					console.log("this.totalNum",this.totalNum)
						if (this.totalNum < 2) {
						this.loads = false
						this.loadtxt = '没有更多数据了'
					} else {
						this.loads = true
						this.loadtxt = '正在加载中'
					}
					this.wdLens = res.meta.total;																																			
				}).catch((error) => {
				
					this.$vux.loading.hide()
					this.wdLens = 0
				})																															
			},
			clearShow() {
				this.bottomCount = ls.get('botnum')
				this.$emit('clearShowDesc')
			}

		}

	}
</script>

<style scoped lang="less">
	.selectNews4Wrap {
		width: 100%;
		height: auto;
		background: #fff;
		padding: 0 0;
		box-sizing: border-box;

		.top {
			width: 100%;
			height: 0.8rem;
			line-height: 0.8rem;
			padding: 0 0.3rem;
			box-sizing: border-box;

			.imgIcon {
				width: 0.4rem;
				height: 0.4rem;
			}

			.univTitle {
				text-align: center;
				font-size: 0.4rem;
				font-weight: bold;
				color: rgba(34, 34, 34, 1);

			}
		}

		.wdBoxOutWrap {
			width: 100%;
			height: auto;
			padding: 0 0.3rem;
			box-sizing: border-box;

			.wdBoxOuter {
				font-size: 0.3rem;
			}
		}
	}
</style>
